const service = require('./groupBuyService.js');
const comm = require('../../../utils/common.js');
const utilService = require('../../../utils/utilService.js');
var app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    scrollHeight: comm.screenHeight - 200,
    screenWidth: comm.screenWidth,
    bannerImage: 'http://img04.tooopen.com/images/20130712/tooopen_17270713.jpg',
    //商品列表数据源
    dataSource: [],
    groupUrls: [],
    shareMsg:"",
  },
  //团详情
  showDetail: function (e) {
    console.log(e.currentTarget.dataset)
    var goodsId = this.data.dataSource[e.currentTarget.dataset.idx].goodsId
    var groupBuyId = this.data.dataSource[e.currentTarget.dataset.idx].groupBuyId
    var groupBuyUserId = this.data.dataSource[e.currentTarget.dataset.idx].groupBuyUserId
    var groupTextId = this.data.dataSource[e.currentTarget.dataset.idx].groupTextId
    var groupText = this.data.dataSource[e.currentTarget.dataset.idx].groupText
    if (parseInt(groupTextId) === 0) {
      wx.showToast({
        title: groupText,
        icon: 'none'
      })
      return;
    }
    if (groupBuyUserId != "" && groupBuyUserId != null){
      wx.navigateTo({
        url: './gBDetail/gBDetail?gBId=' + groupBuyId + "&gBUId=" + groupBuyUserId + "&goodsId=" + goodsId,
      })
    }else{
      wx.navigateTo({
        url: './groupBuyGoodsDetail/groupBuyGoodsDetail?gBId=' + groupBuyId + "&gBUId=" + groupBuyUserId + "&goodsId=" + goodsId,
      })
    }
  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var self = this;
    //授权
    self.authAlert = self.selectComponent("#authAlert");
    self.authAlert.bindCatch()
    app.loginPromise.then(() => {
      //轮播图
      self.banner = self.selectComponent("#banner");
      self.banner.loadData(2);
      self.banner.setSize('100%', '368rpx');
      self.getShareMessage(3);
      service.groupbuyList(function (res) {
        self.setData({
          dataSource: res.groupBuyGoodsVos,
          bannerImage: res.activeImgUrl,
          groupUrls: res.groupUrls
        })
      })
    });
  },
  
  authSuccess() {
    this.getShareMessage(2);
    var self = this;
    //轮播图
    this.banner = this.selectComponent("#banner");
    this.banner.loadData(2);
    self.banner.setSize('100%', '368rpx');
    service.groupbuyList(function (res) {
      self.setData({
        dataSource: res.groupBuyGoodsVos,
        bannerImage: res.activeImgUrl,
        groupUrls: res.groupUrls
      })
    })
  },

  switchValueChanged: function (e) {
    console.log(e.detail.value)
  },

  joinWx: function () {
    let imgUrl = 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1525769195580&di=9ddd3e5ce14c90d1fcaee19d894c6021&imgtype=0&src=http%3A%2F%2Fwww.jituwang.com%2Fuploads%2Fallimg%2F120922%2F234819-1209220Z91874.jpg';
    wx.navigateTo({
      url: '/pages/sendBook/myPublicWalfare/myPublicWalfare?isShow=false&imgUrl=' + encodeURIComponent(imgUrl),
    })
  },
  didTapBanner(res) {
    console.log('banner page =', res.detail)
    wx.navigateTo({
      url: '../../' + res.detail,
    })
  },

  //获取分享信息
  getShareMessage: function (type) {
    var self = this;
    utilService.shareSettings(type,
      function (data) {
        console.log("分享数据", data)
        self.setData({
          shareMsg: data.title
        })
      },
      function (error) {

      }
    );
  },
  onPullDownRefresh:function(){
    var self = this;
    service.groupbuyList(function (res) {
      setTimeout(function () {
        wx.stopPullDownRefresh();
      }, 1000);
      self.setData({
        dataSource: res.groupBuyGoodsVos,
        bannerImage: res.activeImgUrl,
        groupUrls: res.groupUrls
      })
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var title = this.data.shareMsg
    title = title.replace(/#/, app.globalData.userInfo.nickName)
    var param = '?userId=' + getApp().globalData.userId
    var path = 'redPacket/pages/groupBuy/groupBuy' + param
    return {
      title: title,
      path: path,
      success: function (res) {
        // 需要在页面onLoad()事件中实现接口
        wx.showShareMenu({
          // 要求小程序返回分享目标信息
          withShareTicket: true
        });
      },
      fail: function (res) {
      }
    }
  }
})